<?php
	
	class AnswerController extends AuthController{
		
		/**
		 * 返回所有的回答
		 */
		public function index(){
			$model = M('answer');
			$result = $model->query("SELECT * FROM hd_answer AS an JOIN hd_user as u ON an.uid=u.uid");
			$this->assign('result',$result);
			$this->display();
		}
		/**
		 * 删除相应的回答
		 * 扣除回答的相应的金币
		 * 扣除提问人相应的金币
		 */
		public function del(){
			$anid = $_GET['anid'];
			//判断是否为已经采纳的回答
			$accept = isset($_GET['accept'])?$_GET['accept']:0;
			$model = M('answer');
			//查询相应提问的UID 和 asid问题ID
			$ids = current($model->query("SELECT uid,asid FROM hd_answer WHERE anid={$anid}"));

			if($accept){
				//如果为采纳的回答扣队提问发表人的金币 和回答发表人的金币	
				//提问被删除扣除的金币数
				$del1 = $this->getRule()['DELACCPETASKMAN'];
				$del2 = $this->getRule()['DELACCPETANSWERMAN'];			
				//删除回答人相应的金币数
				$dels = $model->_exec("UPDATE hd_user SET point=point-{$del2} WHERE uid={$ids['uid']}");
				//删除发表提问人的相应金币数
				$asuid = current($model->query("SELECT uid FROM hd_ask WHERE asid={$ids['asid']}"))['uid'];
				$model->_exec("UPDATE hd_user SET point=point-{$del1} WHERE uid={$asuid}");
				//再将已采纳的slove字段设置为0表示没有被采纳
				$model->_exec("UPDATE hd_ask SET solve=0 WHERE asid={$ids['asid']}");
				
			}else{
				//如果为不是采纳的回答只扣除发表人的金币
				//提问被删除扣除的金币数
				$delanswer = $this->getRule()['DELANSWER'];			
				//删除相应的金币数
				$dels = $model->_exec("UPDATE hd_user SET point=point-{$delanswer} WHERE uid={$ids['uid']}");
			}
			$result = $model->_exec("DELETE FROM hd_answer WHERE anid={$anid}");
			if($result){
				$this->success('这个回答删除成功~!',__APP__.'?c=answer');
			}
		}
		
		/**
		 * 显示未采纳的回答
		 */
		 public function noaccept(){
			$model = M('answer');
			$result = $model->query('SELECT * FROM hd_answer AS an JOIN hd_user as u ON an.uid=u.uid WHERE an.accept=0');
			$this->assign('result',$result);
			$this->display();
		 }
		 /**
		  * 显示所有已采纳的回答
		  */
		 public function accept(){
			$model = M('answer');
			$result = $model->query('SELECT * FROM hd_answer AS an JOIN hd_user as u ON an.uid=u.uid WHERE an.accept=1');
			$this->assign('result',$result);
			$this->display();
		 }
		
		
	}
